package com.形为型模式.策略模式;

import javax.security.auth.kerberos.KerberosKey;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 制作大闸蟹
 * 抽象策略类
 */
interface CrabCooking{
    void cookingMehtod();
}
/**
 * 清蒸大闸蟹
 * 具体策略类
 */
class SteamedCrabs extends JLabel implements   CrabCooking{
    private static final long serialVersionUID = 1L;

    @Override
    public void cookingMehtod() {
        System.out.println("清蒸最好吃");
        this.setIcon(new ImageIcon("src/img/SteamedCrabs.jpg"));
        this.setHorizontalAlignment(CENTER);
    }
}

/**
 * 红烧大闸蟹
 * 具体策略类
 */
class BraisedCrabs  extends JLabel implements  CrabCooking{
    private static final long serialVersionUID = 1L;
    @Override
    public void cookingMehtod() {
        System.out.println("红烧最好吃");
        this.setIcon(new ImageIcon("src/img/BraisedCrabs.jpg"));
        this.setHorizontalAlignment(CENTER);

    }
}

/**
 * 环境类 厨房
 */
class Kitchen{
    private CrabCooking crabCooking;
    void cookingMehod(){
        crabCooking.cookingMehtod();
    }

    public CrabCooking getCrabCooking() {
        return crabCooking;
    }

    public void setCrabCooking(CrabCooking crabCooking) {
        this.crabCooking = crabCooking;
    }
}
/**
 * 大闸蟹策略
 */
public class StragegyCrab  implements  ItemListener{
    private JFrame f;
    private JRadioButton qz,hs;
    private JPanel CenterJP,SouthJP;
    private Kitchen cf;    //厨房
    private CrabCooking qzx,hsx;    //大闸蟹加工者
    StragegyCrab()
    {
        f=new JFrame("策略模式在大闸蟹做菜中的应用");
        f.setBounds(100,100,500,400);
        f.setVisible(true);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        SouthJP=new JPanel();
        CenterJP=new JPanel();
        f.add("South",SouthJP);
        f.add("Center",CenterJP);
        qz=new JRadioButton("清蒸大闸蟹");
        hs=new JRadioButton("红烧大闸蟹");
        qz.addItemListener(this);
        hs.addItemListener(this);
        ButtonGroup group=new ButtonGroup();
        group.add(qz);
        group.add(hs);
        SouthJP.add(qz);
        SouthJP.add(hs);
        //---------------------------------
        cf=new Kitchen();    //厨房
        qzx=new SteamedCrabs();    //清蒸大闸蟹类
        hsx=new BraisedCrabs();    //红烧大闸蟹类
    }
    public void itemStateChanged(ItemEvent e)
    {
        JRadioButton jc=(JRadioButton) e.getSource();
        if(jc==qz)
        {
            cf.setCrabCooking(qzx);
            cf.cookingMehod(); //清蒸
        }
        else if(jc==hs)
        {
            cf.setCrabCooking(hsx);
            cf.cookingMehod(); //红烧
        }
        CenterJP.removeAll();
        CenterJP.repaint();
        CenterJP.add((Component)cf.getCrabCooking());
        f.setVisible(true);
    }
    public static void main(String[] args) {
        Kitchen kitchen = new Kitchen();
        CrabCooking crabCooking = new SteamedCrabs();
        kitchen.setCrabCooking(crabCooking);
        kitchen.cookingMehod();
        crabCooking = new BraisedCrabs();
        kitchen.setCrabCooking(crabCooking);
        kitchen.cookingMehod();
        new StragegyCrab();
    }
}
